<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="酒店代码" prop="hotalNo">
        <el-input
          v-model="queryParams.hotalNo"
          placeholder="请输入酒店代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="酒店名称" prop="hotalName">
        <el-input
          v-model="queryParams.hotalName"
          placeholder="请输入酒店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="所在城市">-->
<!--        <el-cascader-->
<!--          :options="hotAreaList"-->
<!--          :props="{ checkStrictly: true }"-->
<!--          clearable></el-cascader>-->
<!--      </el-form-item>-->
      <el-form-item label="营业状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择营业状态" clearable>
          <el-option
            v-for="dict in dict.type.hot_hotal_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="酒店类型" prop="hotalType">
        <el-select v-model="queryParams.hotalType" placeholder="请选择酒店类型" clearable>
          <el-option
            v-for="dict in dict.type.hot_hotal_hotal_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="创建时间" prop="createTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.createTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择创建时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="修改时间" prop="updateTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.updateTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择修改时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotal:hotal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hotal:hotal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hotal:hotal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotal:hotal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hotalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门店id" align="center" prop="hotalId" />
      <el-table-column label="酒店代码" align="center" prop="hotalNo" />
      <el-table-column label="酒店名称" align="center" prop="hotalName" />
      <el-table-column label="所在城市" align="center" prop="city" />
      <el-table-column label="归属运营区域" align="center" prop="area" />
      <el-table-column label="归属品牌" align="center" prop="brandName" />
      <el-table-column label="营业状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hot_hotal_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="酒店类型" align="center" prop="hotalType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hot_hotal_hotal_type" :value="scope.row.hotalType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hotal:hotal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotal:hotal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门店管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="酒店代码" prop="hotalNo">
          <el-input v-model="form.hotalNo" placeholder="请输入酒店代码" />
        </el-form-item>
        <el-form-item label="酒店名称" prop="hotalName">
          <el-input v-model="form.hotalName" placeholder="请输入酒店名称" />
        </el-form-item>
        <el-form-item label="营业状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择营业状态">
            <el-option
              v-for="dict in dict.type.hot_hotal_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客房数" prop="num">
          <el-input v-model="form.num" placeholder="请输入客房数" />
        </el-form-item>
        <el-form-item label="酒店类型" prop="hotalType">
          <el-select v-model="form.hotalType" placeholder="请选择酒店类型">
            <el-option
              v-for="dict in dict.type.hot_hotal_hotal_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="酒店档次" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择酒店档次">
            <el-option
              v-for="dict in dict.type.hot_hotal_grade"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="酒店电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入酒店电话" />
        </el-form-item>
        <el-form-item label="是否星级酒店" prop="isStar">
          <el-radio-group v-model="form.isStar">
            <el-radio
              v-for="dict in dict.type.hot_hotal_is_star"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="经营类型" prop="operateType">
          <el-select v-model="form.operateType" placeholder="请选择经营类型">
            <el-option
              v-for="dict in dict.type.hot_hotal_operate_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否提供餐食" prop="isFood">
          <el-radio-group v-model="form.isFood">
            <el-radio
              v-for="dict in dict.type.hot_hotal_is_food"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="酒店介绍">
          <editor v-model="form.introduce" :min-height="192"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHotal, getHotal, delHotal, addHotal, updateHotal } from "@/api/hotal/hotal/hotal";
import { listHotalList,gethotalById } from "@/api/hotal/hotal/hotal";
export default {
  name: "Hotal",
  dicts: ['hot_hotal_status', 'hot_hotal_grade', 'hot_hotal_is_star', 'hot_hotal_hotal_type', 'hot_hotal_is_food', 'hot_hotal_operate_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店管理表格数据
      hotalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hotalId: null,
        hotalNo: null,
        hotalName: null,
        city: null,
        areaId: null,
        brandId: null,
        status: null,
        num: null,
        hotalType: null,
        createTime: null,
        updateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      hotAreaList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门店管理列表 */
    getList() {
      this.loading = true;
      console.log('查询参数:', this.queryParams); // 调试：打印参数检查
      // 确保 hotalId 为数字或 null
      if (this.queryParams.hotalId !== null) {
        this.queryParams.hotalId = Number(this.queryParams.hotalId);
      }
      listHotalList(this.queryParams).then(response => {
        this.hotalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hotalId: null,
        hotalNo: null,
        hotalName: null,
        city: null,
        brandId: null,
        status: null,
        num: null,
        hotalType: null,
        grade: null,
        address: null,
        longitude: null,
        latitude: null,
        phone: null,
        isStar: null,
        operateType: null,
        isFood: null,
        introduce: null,
        userId: null,
        phonenumber: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hotalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加门店管理";
      this.$router.push({path:"/hotal/hotal/edit"})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // 页面路由跳转
      this.$router.push({path:"/hotal/hotal/edit?hotalId="+row.hotalId})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hotalId != null) {
            updateHotal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHotal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hotalIds = row.hotalId || this.ids;
      this.$modal.confirm('是否确认删除门店管理编号为"' + hotalIds + '"的数据项？').then(function() {
        return delHotal(hotalIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hotal/hotal/export', {
        ...this.queryParams
      }, `hotal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

